/*****************************************
* Purpose: calculate 95% exact CI for Rate
* Date: 20230504
******************************************/

%macro calRateCI(nt=          /*N of total*/
				 ,np=		   /*N of positive*/
				 )/des="calculate 95% CI";
	data __result;
		length nt np 8. type $10. p lower upper 8.;
		nt=&nt;
		np=&np;
		p=np/nt;
		type="Wald";
		lower=p-1.96*sqrt(p*(1-p)/nt);
		upper=p+1.96*sqrt(p*(1-p)/nt);
		output;
		type="Exact";
		lower=betainv(0.05/2,np,nt-np+1);
		upper=betainv(1-0.05/2,np+1,nt-np);
		output;
		label nt="N of Total" np="N of Positive" type="Type of 95%CI"
			  p="Rate of positive" lower="lower 95% CI" upper="upper 95% CI";
	run;
	proc print label noobs;run;
	proc datasets lib=work mt=data nodetails nolist nowarn;
		delete __result;
	quit;
%mend;